<?php

namespace Modules\Shop\Api;

use App\Models\Site;
use Auth;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Shop\Entities\Comment;
use Modules\Shop\Transformers\CommentResource;
use Modules\Shop\Entities\Goods;

/**
 * 评论
 * @package Modules\Shop\Api
 */
class CommentController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth:sanctum'])->only(['store']);
    }

    public function index(Site $site, Goods $good)
    {
        $comments = $good->comments()->whereNull('comment_id')->with(['user', 'replys.user'])
            ->latest()->paginate(10);
        return CommentResource::collection($comments);
    }

    public function store(Request $request, Site $site, Goods $good, Comment $comment)
    {
        $comment->site_id = $site['id'];
        $comment->user_id = Auth::id();
        $comment->goods_id = $good['id'];
        $comment->fill($request->input())->save();
        return new CommentResource($comment->load(['user', 'replys.user']));
    }


    public function show($id)
    {
        return view('shop::show');
    }

    public function update(Request $request, $id)
    {
    }

    public function destroy($id)
    {
    }
}
